
#include <BSystem>
#include "member_BMainWindow.h"
#include "member_BWidget.h"

using namespace BWE;

const char* member_BMainWindow::ButtonName(Button button)
{
	switch (button)
	{
	case Button_Abort:
		return "abort";
	case Button_Help:
		return "help";
	case Button_Close:
		return "close";
	case Button_Minimize:
		return "min";
	case Button_Maximize:
		return "max";
	case Button_Normalize:
		return "normal";
	default:
		return 0;
	}
	return 0;
}
Graph member_BMainWindow::ButtonGraph(Button button)
{
	switch (button)
	{
	case Button_Close:
		return Graph_Cross;
	case Button_Minimize:
		return Graph_Minimize;
	case Button_Maximize:
		return Graph_Maximize;
	case Button_Normalize:
		return Graph_Normalize;
	default:
		return Graph_None;
	}
	return Graph_None;
}

member_BMainWindow::member_BMainWindow(BMainWindow* mainWindow)
{
	boss = mainWindow;
	parent = mainWindow->parent();
	resizePart = Part_None;
	titleVisible = true;
	titleHeight = 0;
	titleStyle = 0;
	autoTop = true;
	hoverTitle = false;
	pressed = false;
	dirty = true;
	centering = true;
	adapting = true;
	showState = Show_None;
	alignment.ia_align = Align_LeftCenter;
	alignment.ia_spacing = 1;
	buttonAlign = Align_RightCenter;
	buttonLayout = new BHBoxLayout("buttons", boss, Part_None);
}
member_BMainWindow::~member_BMainWindow()
{

}

bool member_BMainWindow::resizable()
{
	if (widget_member(boss)->parent)
	{
		if (BLayout* layout = widget_member(boss)->upperLayout)
			return false;
		return true;
	}
	if (widget_member(boss)->pixmap || widget_member(boss)->frameless)
		return true;
	return false;
}
void member_BMainWindow::adaptSize()
{
	BWidget* parent = boss->parent();
	if (!parent || !boss->fettered() || boss->sizePolicy().empty())
	{
		BRect upperRect;
		BWidget* root = boss->root();
		if (root != boss)
		{
			IWindow* window = widget_member(root)->window;
			if (window && window->popupWidgets.contain(boss))
				upperRect = root->mapTo(parent, root->size());
			else
				upperRect = parent->size();
		}
		else
		{
			if (widget_member(boss)->window)
			{
				const BScreen* screen = BSystem::GetScreen(BCursor::GetPos());
				upperRect = screen->rect();
			}
			else if (BPixmap* pixmap = widget_member(boss)->pixmap)
			{
				upperRect = pixmap->size();
			}
		}
		if (upperRect.valid())
		{
			BSize size = boss->size();
			BSize hsize = boss->sizeHint();
			if (size.width() < hsize.width())
				size.width() = hsize.width();
			if (size.width() > upperRect.width())
				size.width() = upperRect.width();
			if (size.height() < hsize.height())
				size.height() = hsize.height();
			if (size.height() > upperRect.height())
				size.height() = upperRect.height();
			boss->setSize(size);
		}
	}
}
void member_BMainWindow::centerAlign()
{
	BWidget* parent = boss->parent();
	if (!parent || !boss->fettered() || boss->sizePolicy().empty())
	{
		BRect upperRect;
		BWidget* root = boss->root();
		if (root != boss)
		{
			IWindow* window = widget_member(root)->window;
			if (window && window->popupWidgets.contain(boss))
				upperRect = root->mapTo(parent, root->size());
			else
				upperRect = parent->size();
		}
		else
		{
			if (widget_member(boss)->window)
			{
				const BScreen* screen = BSystem::GetScreen(BCursor::GetPos());
				upperRect = screen->rect();
			}
			else if (BPixmap* pixmap = widget_member(boss)->pixmap)
			{
				upperRect = pixmap->size();
			}
		}
		if (upperRect.valid())
		{
			BRect rect = boss->rect();
			rect = upperRect.align(Align_Center, rect.size());
			if (!upperRect.contain(rect))
				rect = upperRect.align(Align_Center, rect.size());
			boss->setRect(rect);
		}
	}
}
void member_BMainWindow::freshRects()
{
	const BFont* titleFont = 0;
	if (titleStyle)
		titleFont = titleStyle->value(Value_Font);
	if (!titleFont)
		titleFont = boss->realStyle()->font();

	BSize buttonSize = buttonLayout->sizeHint();
	BPerch perch = boss->perch();
	if (autoTop)
	{
		perch.top() = boss->preferSize(Part_Top).height();
		if (widget_member(boss)->pixmap || boss->parent() || boss->frameless())
		{
			int realTitleHeight = bMax(titleHeight, titleIconSize.height());
			if (realTitleHeight < titleFont->size())
				realTitleHeight = titleFont->size();
			if (realTitleHeight < buttonSize.height())
				realTitleHeight = buttonSize.height();
			realTitleHeight += titleMargin.height();
			if (perch.top() < realTitleHeight)
				perch.top() = realTitleHeight;
			buttonLayout->setVisible(true);
		}
		else
		{
			buttonLayout->setVisible(false);
		}
	}
	boss->setPerch(perch);

	int w = boss->width();
	int h = boss->height();
	int b = boss->border();

	titleRect = boss->rect(Part_Top);
	titleRect.top() += titleMargin.top();
	titleRect.left() += titleMargin.left();
	titleRect.right() -= titleMargin.right();
	titleRect.bottom() -= titleMargin.bottom();
	borderLeft.set(0, b, b, h - b - b);
	borderTop.set(b, 0, w - b - b, b);
	borderRight.set(w - b, b, b, h - b - b);
	borderBottom.set(b, h - b, w - b - b, b);
	cornerLeftTop.set(0, 0, b, b);
	cornerLeftBottom.set(0, h - b, b, b);
	cornerRightTop.set(w - b, 0, b, b);
	cornerRightBottom.set(w - b, h - b, b, b);

	buttonLayout->setRect(titleRect.align(buttonAlign, buttonSize));
	buttonLayout->adjust();

	BRect iconArea(titleRect.pos());
	iconArea.setHeight(titleRect.height());
	BSize realIconSize;
	if (BIcon* icon = boss->icon())
	{
		iconArea.setWidth(bMax(titleRect.height(), titleIconSize.width()));
		realIconSize = titleIconSize;
		if (BImage* image = icon->image(State_Normal))
		{
			if (realIconSize.width() < 0)
				realIconSize.width() = image->width();
			if (realIconSize.height() < 0)
				realIconSize.height() = image->height();
		}
		if (realIconSize.width() < 1)
			realIconSize.width() = titleRect.height();
		if (realIconSize.height() < 1)
			realIconSize.height() = titleRect.height();
		realIconSize = iconArea.wrap(realIconSize);
	}
	alignment.ia_iconSize = realIconSize;

	if (widget_member(boss)->t_title.size())
		alignment.ia_textSize = titleFont->textSize(widget_member(boss)->t_title);
	else
		alignment.ia_textSize = titleFont->textSize(widget_member(boss)->title);
	alignment.adjust(titleRect);
}

void member_BMainWindow::slotParentChanged(BObject* object, const BValue& value)
{
	BWidget* parent = value;
	if (this->parent != parent)
	{
		if (this->parent)
		{
			disconnect(this->parent, Signal_Resized);
		}
		this->parent = parent;
		centering = true;
		if (this->parent)
		{
			connect(this->parent, Signal_Resized, &member_BMainWindow::slotParentResized);
		}
	}
}
void member_BMainWindow::slotParentResized(BObject* object, const BValue& value)
{
	if (showState == Show_Maximized)
	{
		const BSize& size = value;
		boss->setSize(size);
	}
}
void member_BMainWindow::slotButtonClicked(BButton* button, const BValue& value)
{
	for (int i = 0; i < buttonLayout->count(); i++)
	{
		BWidget* widget = buttonLayout->widget(i);
		if (widget == button)
		{
			Button btn = button->asset();
			if (btn == Button_Hide)
				boss->hide();
			if (btn == Button_Close)
				boss->close();
			if (btn == Button_Minimize)
				boss->show(Show_Minimized);
			if (btn == Button_Maximize)
			{
				if (button->checked())
					boss->show(Show_Maximized);
				else
					boss->show(Show_Normal);
			}
			break;
		}
	}
}
